﻿using ACC.Extensions.ClientBusiness;
using ACC.Extensions.Entities;
using ACC.Extensions.PCClient.BaseWindow;
using ACC.Extensions.PCClient.Helper;
using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ACC.Extensions.PCClient.View
{
    /// <summary>
    /// Interaction logic for ConfigPicUploadWnd.xaml
    /// </summary>
    public partial class ConfigPicUploadWnd :BaseCustomWindow
    {
        public ConfigPicUploadWnd()
        {
            InitializeComponent();
            this.Owner = Application.Current.MainWindow;
        }

        private void BaseMetroWindow_Loaded(object sender, RoutedEventArgs e)
        {
            while (UISession.PartTypeList==null)
            {
                Thread.Sleep(500);
            }
            this.cmbPartType.ItemsSource = UISession.PartTypeList;
            this.cmbPartType.SelectedValuePath = "PartNO";
            this.cmbPartType.DisplayMemberPath = "PartNO";
        }

        private void btnUploadFile_Click(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog()
            {
                Filter = "(*.jpg,*.png,*.jpeg,*.bmp,*.gif)|*.jgp;*.png;*.jpeg;*.bmp;*.gif|All files(*.*)|*.*"
            };
            var result = openFileDialog.ShowDialog();
            if (result == true)
            {
                string strFilePath = openFileDialog.FileName;
                this.txtFilePath.Text = strFilePath;
                double dHeight;
                double dWidth;
                ClientHelper.LoadImageSource(strFilePath, this.imgPartPic, out dHeight, out dWidth);
                this.cvsImg.Width = dWidth;
                this.cvsImg.Height = dHeight;
            }
        }
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(this.cmbPartType.SelectedValue.ToString())
                || string.IsNullOrEmpty(this.txtPicSubNO.Text)
                || string.IsNullOrEmpty(this.txtRegion1LeftUpPosition.Text)
                || string.IsNullOrEmpty(this.txtRegion1LeftUpCoord.Text)
                || string.IsNullOrEmpty(this.txtRegion1RightDownPosition.Text)
                || string.IsNullOrEmpty(this.txtRegion1RightDownCoord.Text)
                || string.IsNullOrEmpty(this.txtRegion2LeftUpPosition.Text)
                || string.IsNullOrEmpty(this.txtRegion2LeftUpCoord.Text)
                || string.IsNullOrEmpty(this.txtRegion2RightDownPosition.Text)
                || string.IsNullOrEmpty(this.txtRegion2RightDownCoord.Text))
            {
                MessageBox.Show("图片信息不全，请输入。");
                this.DialogResult = false;
                return;
            }
            else
            {
                AddConfigPicInfo();
                this.DialogResult = true;
            }
        }

        //添加图片信息
        private void AddConfigPicInfo()
        {
            if (UISession.PartTypePicList == null)
            {
                UISession.PartTypePicList = new List<PartTypePic>();
            }

            string strSelPartNO = this.cmbPartType.SelectedValue.ToString().Trim();
            int inSelPicSubNO = Convert.ToInt32(this.txtPicSubNO.Text.Trim());
            if (string.IsNullOrEmpty(strSelPartNO))
            {
                return;
            }

            PartTypePic pic = UISession.PartTypePicList.Find(x => x.PartNo == strSelPartNO && x.PicSubNo ==  inSelPicSubNO);

            string strFilePath = this.txtFilePath.Text.ToString();
            string strPicName = strFilePath.Substring(strFilePath.LastIndexOf('\\')+1,strFilePath.Length-strFilePath.LastIndexOf('\\')-1);
            if (pic == null)
            {
                pic = new PartTypePic();
                GridPicDetail gridPicInfo = new GridPicDetail();
                pic.PartTypeId= UISession.PartTypeList.FirstOrDefault(x => x.PartNo == strSelPartNO).PartTypeId;
                pic.GridPicName = strPicName;
                pic.PartNo = strSelPartNO;
                pic.PicSubNo = txtPicSubNO.Text == "" ? 0 : int.Parse(txtPicSubNO.Text);
                gridPicInfo.Region1LeftUpPosition = this.txtRegion1LeftUpPosition.Text.ToString().Trim();
                gridPicInfo.Region1LeftUpCoord = this.txtRegion1LeftUpCoord.Text.ToString().Trim();
                gridPicInfo.Region1RightDownPosition = this.txtRegion1RightDownPosition.Text.ToString().Trim();
                gridPicInfo.Region1RightDownCoord = this.txtRegion1RightDownCoord.Text.ToString().Trim();

                gridPicInfo.Region2LeftUpPosition = this.txtRegion2LeftUpPosition.Text.ToString().Trim();
                gridPicInfo.Region2LeftUpCoord = this.txtRegion2LeftUpCoord.Text.ToString().Trim();
                gridPicInfo.Region2RightDownPosition = this.txtRegion2RightDownPosition.Text.ToString().Trim();
                gridPicInfo.Region2RightDownCoord = this.txtRegion2RightDownCoord.Text.ToString().Trim();
 
                gridPicInfo.FilePath = strFilePath;
                pic.GridPicInfo = gridPicInfo;
                UISession.PartTypePicList.Add(pic);
            }
            else
            {
                PartTypePic newPic = new PartTypePic();
                newPic.PartTypeId= UISession.PartTypeList.FirstOrDefault(x => x.PartNo == strSelPartNO).PartTypeId;
                newPic.GridPicName = strPicName;
                newPic.PartNo = strSelPartNO;
                newPic.PicSubNo = txtPicSubNO.Text == "" ? 0 : int.Parse(txtPicSubNO.Text);
                GridPicDetail gridPicInfo = new GridPicDetail();
                gridPicInfo.Region1LeftUpPosition = this.txtRegion1LeftUpPosition.Text.ToString().Trim();
                gridPicInfo.Region1LeftUpCoord = this.txtRegion1LeftUpCoord.Text.ToString().Trim();
                gridPicInfo.Region1RightDownPosition = this.txtRegion1RightDownPosition.Text.ToString().Trim();
                gridPicInfo.Region1RightDownCoord = this.txtRegion1RightDownCoord.Text.ToString().Trim();

                gridPicInfo.Region2LeftUpPosition = this.txtRegion2LeftUpPosition.Text.ToString().Trim();
                gridPicInfo.Region2LeftUpCoord = this.txtRegion2LeftUpCoord.Text.ToString().Trim();
                gridPicInfo.Region2RightDownPosition = this.txtRegion2RightDownPosition.Text.ToString().Trim();
                gridPicInfo.Region2RightDownCoord = this.txtRegion2RightDownCoord.Text.ToString().Trim();
                gridPicInfo.FilePath = strFilePath;
                newPic.GridPicInfo = gridPicInfo;
                UISession.PartTypePicList.RemoveAll(x => x.PartNo == strSelPartNO && x.PicSubNo == inSelPicSubNO);
                UISession.PartTypePicList.Add(newPic);
            }
            //存储图片到_Pic文件夹
            ClientSettingHelper.StorePic(strFilePath, strPicName);
            //报存图片到配置信息
            bool result = ClientSettingHelper.WritePicList(UISession.PartTypePicList);

            if (result)
            {
                MessageDialogWnd.ShowDialog(
                     LanguageHelper.LoadStringFromResource("TITLE_MESSAGEDIALOG"),
                     LanguageHelper.LoadStringFromResource("MSG_STORE_SUCCESS"));
            }
            else
            {
                MessageDialogWnd.ShowDialog(
                    LanguageHelper.LoadStringFromResource("TITLE_ERROR"),
                    LanguageHelper.LoadStringFromResource("MSG_ERR_SAVESETTING"));
            }
        }
        public void DisplayPartTypePic(PartTypePic partTypePic)
        {
            string strFilePath = AppDomain.CurrentDomain.BaseDirectory + "_pic\\" + partTypePic.GridPicName;
            if (!File.Exists(strFilePath))
            {
                return;
            }
            this.txtFilePath.Text = strFilePath;
            double dHeight;
            double dWidth;
            ClientHelper.LoadImageSource(strFilePath, this.imgPartPic,out dHeight,out dWidth );
            this.cvsImg.Width = dWidth;
            this.cvsImg.Height = dHeight;
            this.cmbPartType.SelectedValue=partTypePic.PartNo;
            this.txtPicSubNO.Text = partTypePic.PicSubNo.ToString();

            this.txtRegion1LeftUpPosition.Text = partTypePic.GridPicInfo.Region1LeftUpPosition;
            this.txtRegion1LeftUpCoord.Text = partTypePic.GridPicInfo.Region1LeftUpCoord;
            this.txtRegion1RightDownPosition.Text=partTypePic.GridPicInfo.Region1RightDownPosition;
            this.txtRegion1RightDownCoord.Text=partTypePic.GridPicInfo.Region1RightDownCoord;

            this.txtRegion2LeftUpPosition.Text = partTypePic.GridPicInfo.Region2LeftUpPosition;
            this.txtRegion2LeftUpCoord.Text = partTypePic.GridPicInfo.Region2LeftUpCoord;
            this.txtRegion2RightDownPosition.Text = partTypePic.GridPicInfo.Region2RightDownPosition;
            this.txtRegion2RightDownCoord.Text = partTypePic.GridPicInfo.Region2RightDownCoord;
        }

        public void UploadPicDisable()
        {
            this.txtFilePath.IsEnabled = false;
            this.btnUploadFile.IsEnabled = false;
            this.btnConfirm.IsEnabled = false;
            this.btnCancel.IsEnabled = false;
            this.boderPicInfo.IsEnabled = false;
        }

        private void BaseCustomWindow_Activated(object sender, EventArgs e)
        {
            UISession.IsWndActivated = this.IsActive;
        }

        private void BaseCustomWindow_Deactivated(object sender, EventArgs e)
        {
            UISession.IsWndActivated = this.IsActive;
        }

        private void Canvs_MouseMove(object sender, MouseEventArgs e)
        {
            if(_bCursorPoint)
            {
                System.Windows.Point point = e.GetPosition((IInputElement)sender);
                this.txtCursorPoint.Text = point.X.ToString("f0")+","+point.Y.ToString("f0");
            }
        }
        private bool _bCursorPoint = true;
        private void Canvs_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            if(_bCursorPoint == false)
            {
                _bCursorPoint = true;
            }
            else
            {
                _bCursorPoint = false;
            }
        }

    }

}
